package com.game.majiang.logic;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.comm.ISession;
import com.game.majiang.config.DataManager;
import com.game.majiang.config.LogManager;
import com.game.majiang.vo.RobotVo;
import com.game.majiang.vo.UserVo;
import com.game.server.db.entity.UserInfoData;
import com.game.server.system.AbstractClient;
import com.game.server.system.AbstractPlayer;
import com.game.server.system.AbstractRoom;
import com.game.server.system.AbstractTable;

import com.game.server.util.Util;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * @author 
 *
 */
@Getter
@ToString
public class LogicClient extends AbstractClient implements Comparable<LogicClient> {

	private UserVo vo;//用户数据

	@Setter
	private int roomid;//房间号

	@Setter
	private int tableid;//桌子号


	@Setter
	private int clubid;//俱乐部ID


	@Setter
	private boolean lost=false;//
	@Setter
	private String equipmentNumber="";//设备号

	//	public long clickmills=0;//毫秒
	public HashMap<Integer, Long> clickmills=new HashMap<Integer, Long>();//协议号-- 毫秒


	public AbstractTable getTable()
	{
		AbstractRoom room = LogicServer.getInstance().getRoom(roomid);

		if(room!=null)
		{
			LogManager.getInstance().Log("房间roomid："+ room.toString());
			return room.getTable(tableid);
		}
		LogManager.getInstance().Log("房间roomid错误： "+tableid);
		return null;
	}

	public AbstractRoom getRoom()
	{
		return LogicServer.getInstance().getRoom(roomid);
	}

	public AbstractPlayer getPlayer()
	{
		AbstractRoom room = LogicServer.getInstance().getRoom(roomid);
		if(room!=null)
		{
			AbstractTable table = room.getTable(tableid);
			if(table!=null)
			{
				return table.getPlayer(getVo().getId());
			}
		}
		return null;
	}

	public LogicClient(int uid, ISession session) {
		super(uid, session);
		vo = new UserVo(uid);
		
	}

	public LogicClient(int uid)
	{
		super(uid);
		vo = new UserVo(uid);
	}
	public LogicClient()
	{
		super();
		vo = new UserVo();
	}
	
	@Override
	public  void Close() 
	{
		LogManager.getInstance().Log("用户："+this.getVo().getNickName()+"  走close"+" 金币:"+this.getVo().getGold()+"  积分:"+this.getVo().getIntegral()+" 钻石:"+this.getVo().getDiamonds());
		super.Close();
		boolean ret = true;
		lost = true;
		if(getTable() == null) {
			LogicServer.getInstance().removeClient(getUserId());
		}
			getVo().Save(getIp());
		UserInfoData infoData = DataManager.getInstance().getMapUserData().get(Integer.valueOf(this.getVo().getId()));
		infoData.setLogintime(new Timestamp(System.currentTimeMillis()));
	}


	@Override
	public int compareTo(LogicClient o) {
		return 0;
	}

	public void checkRecharge()
	{
		ArrayList<Integer> list = getVo().getRecharge();
		int gold = 0;
		for(Integer i:list)
		{
			gold+=i;
		}
		if(gold > 0)
		{	
			UserVo.updateRecharge(getUserId());
			//   			 getVo().AddGold(gold);
			getVo().AddDiamonds(gold);
			getVo().AddIntegral(gold*100);
			getVo().Savenomajin();
		}
	}
	/**
	 * 协议点击是否速度判断两秒一次()
	 * @return
	 */
	public boolean checkClick(int xieyihao) {
		long nowtime=System.currentTimeMillis();
		Iterator<Map.Entry<Integer, Long>> it = clickmills.entrySet().iterator();//上下相同, 这个清晰一些
		while (it.hasNext()) {
			int key=it.next().getKey();
			long value=clickmills.get(key);
			if(xieyihao==key) {
				if(nowtime-value>200) {
					clickmills.put(key, nowtime);
					return true;
				}else {
					return false;
				}
			}
		}
		clickmills.put(xieyihao, nowtime);
		return true;
	}

	public boolean checkkoufei(int uid,int roomtype) {
		long nowtime=System.currentTimeMillis();
//		if(koufeimills.get(uid)!=null) {
//			HashMap<Integer, Long> l=koufeimills.get(uid);
//			if(l.get(roomtype)!=null) {
//				if(l.get(roomtype)-nowtime>0) {
//					return true;
//				}else {
//					return false;
//				}
//			}else {
//				return false;
//			}
//		}else {
//			return false;
//		}
		return true;
	}
	
	public boolean watchingAndEnteringDating() {
		boolean guanzhanBool = false;

		if("1".equals(DataManager.getInstance().systemValue("gaunzhansitdowndating"))) {
			guanzhanBool = true;
		}
		return guanzhanBool;
	}
	
}





